var createError = require("http-errors");
let express = require("express");
let bodyParser = require("body-parser");
let app = express();
var cors = require("cors");
var logger = require("morgan");
var path=require("path")
var cookieParser = require("cookie-parser");
const expressJWT = require("express-jwt");
const config = require("./config");
let { userRouter,infoRouter,clientRouter,itineraryRouter,passengerRouter } = require("./routes/index");
// 处理post请求
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// 打印日志
app.use(logger("dev"));
// 处理跨域
app.use(cors());

// 日志
app.use(logger("dev"));

// 使用cookie
app.use(cookieParser());

// 校验token，获取headers⾥里里的Authorization的token，要写在路由加载之前，静态资源之后
app.use(
  expressJWT({
    secret: config.Secret,
    algorithms: ["HS256"],
    credentialsRequired: true,
  }).unless({
    path: ["/api/user/register", "/api/user/login"], //⽩白名单,除了了这⾥里里写的地址，其他的URL都需要验证
  })
);

app.use("/api", userRouter);
app.use("/api",infoRouter);
app.use("/api",clientRouter);
app.use("/api",itineraryRouter);
app.use('/api',passengerRouter)

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  if (err.name === "UnauthorizedError") {
    // 这个需要根据⾃自⼰己的业务逻辑来处理理
    res.status(401).send({ code: -1, msg: "token验证失败" });
  } else {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get("env") === "development" ? err : {};
    // render the error page
    res.status(err.status || 500);
    res.json({
      message: err.message,
      error: err
    })
  }
});

app.listen(3000, function () {
  console.log("服务在3000启动了");
});
